package com.lgx.right.dao.impl;

import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.lgx.common.basic.dao.GeneralDao;
import com.lgx.common.basic.dao.GeneralJdbcDao;
import com.lgx.common.exception.DataException;
import com.lgx.common.web.Pagination;
import com.lgx.right.dao.IUserDao;
import com.lgx.right.entity.User;

/**
 * 
 * @author acer
 *
 */
@Component("userDao")
public class UserDao extends GeneralDao implements IUserDao {
	
	@Autowired
	private GeneralJdbcDao baseDao;
	
	public GeneralJdbcDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(GeneralJdbcDao baseDao) {
		this.baseDao = baseDao;
	}

	protected Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * 创建用户，
	 * 要求密码加密
	 */
	public User createUser(User user) throws DataException {
		this.getHibernateTemplate().save(user);
		log.info("[创建用户]:"+user.getId());
		return user;
	}

	public User updateUser(User user) throws DataException {
		this.getHibernateTemplate().update(user);
		log.info("[更新用户]:"+user.getId());
		return user;
	}

	public void deleteUser(User user) throws DataException {
		log.info("[删除用户]:"+user.getId());
		this.getHibernateTemplate().delete(user);
	}

	public void deleteUserById(long id) throws DataException {
		log.info("[删除用户]:"+id);
		this.deleteUser(this.findUserById(id));
	}

	public User findUserById(long id) {
		return this.getHibernateTemplate().get(User.class, id);
	}
	
	
	@SuppressWarnings("unchecked")
	public List<User> findUserList(String sql){
		return this.getHibernateTemplate().find(sql);
	}

	@SuppressWarnings("unchecked")
	public List<User> findUserList(String sql,Object[] params, int pageSize, int pageNum) {
		return this.getList(sql, params, pageSize, pageNum);
	}

	public Pagination findUserPagination(String sql,Object[] params, int pageSize, int pageNum) {
		return this.getListPagination(sql, params, pageSize, pageNum);
	}
	
	public Pagination findUserPagination(Object[] params, Class<?> clazz, int pageSize, int pageNum) {
		String sql = "select ue.degree as degree," +
							"ue.level as level," +
							"ue.headimage as headImage," +
							"ue.funtype as funType," +
							"ue.giftrecord as giftRecord," +
							"ue.points as points," +
							"ue.pointrecorddate as pointRecordDate," +
							"u.id as userId," +
							"u.username as username," +
							"u.nickname as nickname," +
							"u.truename as truename," +
							"u.email as email," +
							"u.sex as sex," +
							"u.mobile as mobile," +
							"u.card_id as cardId";
	
		sql += " from ts_user u left join ts_user_expand ue on u.id = ue.user_id where u.isdelete = '0' ";
		
		if(!params[0].equals(""))
		{
			sql += " and u.sex = " + params[0];
		}
		
		if(!params[1].equals(""))
		{
			sql += " and ue.degree = '"+params[1]+"'";
		}
		
		sql += " order by ue.id asc";
		
		return baseDao.queryJdbcListPagination(sql, clazz, pageSize, pageNum);
	}

	@Override
	public Pagination findUserList(String type,long orgId,int usertype,String name, String card,String mobile,
			String registerTimeStar, String registerTimeEnd ,String orgName,String isMaster, int pageSize,int pageNum) {
		String sql = "select u from User u  where u.isDelete = '0'";
		
		if("1".equals(type)){
			//会员查询
			sql += " and u.userType = 1";
			if(usertype == 2 || usertype == 3){
				//处理联盟商家、推广站
				sql += " and u.orgId = " + orgId;
			}else if(usertype == 4){
				//处理推广中心
				sql +=" and exists (from Organization o where o.parent.id = "+orgId+" and o.type in (1,2))";
			}else if(usertype == 5){
				//运营中心、分子公司
				sql +=" and exists (from Organization o where o.parent.parent.id = "+orgId+" and o.type in (1,2))";
			}else if(usertype == 6){
				//总公司
				//sql +=" and exists (from Organization o where o.parent.parent.parent.id = "+orgId+" and o.type in (1,2))";
			}
		}else{
			//用户查询
			if(usertype != 6){
				//处理联盟商家、推广站、推广中心、运营中心、分子公司
				sql += " and u.userType = " + usertype + " and u.orgId = " + orgId;
			}else{
				//总公司
				sql += " and u.userType <> 1 ";
			}
		}
		
		if(!StringUtils.isEmpty(name)){
			sql += " and (u.username like '%"+name+"%' or u.truename like '%"+name+"%')";
		}
		
		if(!StringUtils.isEmpty(mobile)){
			sql += "  and u.mobile like '%"+mobile+"%'";
		}
		
		if(!StringUtils.isEmpty(card)){
			sql += " and u.card like '%"+card+"%'";
		}
		
		if(!StringUtils.isEmpty(registerTimeStar)&&!StringUtils.isEmpty(registerTimeEnd)){
			sql += "  and u.registerTime between "+registerTimeStar+" and "+registerTimeEnd;
		}
		
		if(!StringUtils.isEmpty(isMaster)){
			sql += " and u.isMaster = '"+isMaster+"'";
		}
		
		sql += " order by u.registerTime desc";
		return this.getListPagination(sql, new Object[]{}, pageSize, pageNum);
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public List<User> findUserList(String name, String param) {
		String sql = "from User u where u.isDelete = '0' ";
		sql += " and u."+name+" = '"+param+"'" ;
		return this.getHibernateTemplate().find(sql);
	}

	@Override
	public User loginUser(String username, String password) {
		String sql = "from User u where u.isDelete = '0' ";
		sql += " and (u.username = '"+username+"' or u.mobile = '"+username+"' or u.card.id = '"+username+"')";
		sql += " and u.password = '"+password+"'";
		@SuppressWarnings("unchecked")
		List<User> users = this.getHibernateTemplate().find(sql);
		if(users.size() == 0){
			return null;
		}
		return users.get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findActiveUserList(long num){
		String sql = "from User u order by u.lastLogin desc limit " + num;
		return this.getHibernateTemplate().find(sql);
	}
}
